import os
import io
import pprint
import logging
from logging.handlers import RotatingFileHandler

log_file = os.path.expanduser('~/.my-tools/log/study/study.log')
data_dir = os.path.expanduser('~/.my-tools/data/study/qas/')
os.makedirs(os.path.dirname(log_file), exist_ok=True)
os.makedirs(data_dir, exist_ok=True)

log_formatter = logging.Formatter('[%(asctime)s][%(levelname)s]: %(message)s')
handler = RotatingFileHandler(log_file, maxBytes=100*1024*1024, backupCount=2)  # 100 MB per file, 2 backup
handler.setFormatter(log_formatter)
logger = logging.getLogger('study')
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)

def log_pretty_print(data):
    output = io.StringIO()
    pprint.pprint(data, stream=output)
    contents = output.getvalue()
    output.close()
    return contents